﻿using Common;
using IBLL;
using Microsoft.AspNetCore.Mvc;
using Models;
using Models.DTO;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Unmanned_MSUI.Areas.Admin.Filters;

namespace Unmanned_MSUI.Areas.Admin.Controllers
{
    [Area("Admin")]
    [MyFilter]
    public class RoleInfoController : Controller
    {

        private IRoleInfoBLL _roleInfoBLL;
        private IStaffInfoBLL _staffInfoBLL;
        private IMenuInfoBLL _menuInfoBLL;
        public RoleInfoController(
            IRoleInfoBLL roleInfoBLL
            , IStaffInfoBLL staffInfoBLL
            , IMenuInfoBLL menuInfoBLL
            )
        {
            _roleInfoBLL = roleInfoBLL;
            _staffInfoBLL = staffInfoBLL;
            _menuInfoBLL = menuInfoBLL;
        }

        public IActionResult ListView()
        {
            return View();
        }

        public IActionResult CreateRoleInfoView()
        {
            return View();
        }

        public IActionResult UpdateRoleInfoView()
        {
            return View();
        }

        public IActionResult BindStaffInfoView()
        {
            return View();
        }

        public IActionResult BindMenuInfoView()
        {
            return View();
        }


        #region 角色列表获取的方法(GetRoleInfos)

        /// <summary>
        /// 角色列表获取的方法
        /// </summary>
        /// <param name="page">第几页</param>
        /// <param name="limit">每页数量</param>
        /// <param name="account">用户账号</param>
        /// <param name="userName">用户姓名</param>
        /// <returns></returns>
        [HttpGet]
        public ActionResult GetRoleInfos(int page, int limit, string roleName)
        {
            //数据总条数
            int count;
            //调用BLL的获取用户方法
            List<GetRoleInfoDTO> list = _roleInfoBLL.GetRoleInfos(page, limit, roleName, out count);
            //返回结果
            ReturnResult result = new ReturnResult()
            {
                Code = 0,
                Msg = "获取成功！",
                Data = list,
                IsSuccess = true,
                Count = count
            };
            //返回结果
            return Json(result);
        }

        #endregion

        #region 添加用户的方法(CreateRoleInfo)
        [HttpPost]
        public ActionResult CreateRoleInfo([FromForm] RoleInfo role)
        {
            string msg;
            //调用添加的方法
            bool isSuccess = _roleInfoBLL.CreateRoleInfo(role, out msg);
            //返回结果封装
            ReturnResult result = new ReturnResult();
            result.Msg = msg;
            result.IsSuccess = isSuccess;
            //判断是否成功
            if (isSuccess)
            {
                result.Code = 200;
            }
            return Json(result);
        }


        #endregion

        #region 用户软删除的接口（DeleteRoleInfo）
        /// <summary>
        /// 用户软删除的接口
        /// </summary>
        /// <param name="id">前端传来的Id</param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult DeleteRoleInfo(string id)
        {
            //实例化返回对象
            ReturnResult result = new ReturnResult();
            //非空判断
            if (string.IsNullOrWhiteSpace(id))
            {
                result.Msg = "Id不能为空！";
                return Json(result);
            }
            //调用删除方法
            bool isOK = _roleInfoBLL.DeleteRoleInfo(id);
            //判断删除情况
            if (isOK)
            {
                result.Msg = "删除用户成功！";
                result.Code = 200;
            }
            //返回结果
            return Json(result);
        }
        #endregion

        #region 批量软用户软删除（DeleteRoleInfo）

        [HttpPost]
        public ActionResult DeleteRoleInfos(List<string> ids)
        {
            //实例化返回对象
            ReturnResult result = new ReturnResult();
            //非空判断

            if (ids == null || ids.Count == 0)
            {
                result.Msg = "你还没有选择要删除的用户！";
                return Json(result);
            }
            //调用批量删除的方法
            bool isOK = _roleInfoBLL.DeleteRoleInfo(ids);
            //判断是否成功
            if (isOK)
            {
                result.Msg = "删除成功！";
                result.Code = 200;
            }
            else
            {
                result.Msg = "删除失败！";
            }
            return Json(result);//返回结果
        }

        #endregion

        #region 修改用户接口（UpdateRoleInfo）

        /// <summary>
        /// 修改用户接口
        /// </summary>
        /// <param name="role">前端传递的用户实体</param>
        /// <returns></returns>
        [HttpPost]

        public ActionResult UpdateRoleInfo([FromForm] RoleInfo role)
        {
            //实例化返回对象
            ReturnResult result = new ReturnResult();
            //调用方法
            string msg;
            bool isOK = _roleInfoBLL.UpdateRoleInfo(role, out msg);
            result.Msg = msg;
            //判断是否成功
            if (isOK)
            {
                result.Code = 200;
                result.IsSuccess = isOK;
            }
            return Json(result);//返回结果
        }
        #endregion


        #region 获取用户未绑定用户数据集的接口（GetUserInfoOptions）

        /// <summary>
        /// 获取用户未绑定数九集的接口
        /// </summary>
        /// <param name="releId">要绑定的id</param>
        /// <returns></returns>
        [HttpGet]
        public ActionResult GetUserInfoOptions(string roleId)
        {
            //实例化返回的对象
            ReturnResult result = new ReturnResult();
            if (string.IsNullOrWhiteSpace(roleId))
            {
                result.Msg = "id不能为空！";
                return Json(result);
            }
            //获取用户列表
            List<GetStaffInfoDTO> options = _staffInfoBLL.GetStaffInfos();
            //获取已经绑定的用户id集
            List<string> userIds = _roleInfoBLL.GetBindUserIds(roleId);
            //返回结果
            result.Data = new
            {
                options,
                userIds
            };
            result.Code = 200;
            result.Msg = "获取成功！";
            result.IsSuccess = true;
            return Json(result);
        }


        #endregion

        #region 获取菜单未绑定用户数据集的接口（GetMenuInfoOptions）

        /// <summary>
        /// 获取菜单未绑定数据集的接口
        /// </summary>
        /// <param name="roleId">要绑定的id</param>
        /// <returns></returns>
        [HttpGet]
        public ActionResult GetMenuInfoOptions(string roleId)
        {
            //实例化返回的对象
            ReturnResult result = new ReturnResult();
            if (string.IsNullOrWhiteSpace(roleId))
            {
                result.Msg = "id不能为空！";
                return Json(result);
            }
            //获取用户列表
            List<GetMenuInfosDTO> options = _menuInfoBLL.GetMenuInfo();
            //获取已经绑定的用户id集
            List<string> menuIds = _roleInfoBLL.GetBindMenuIds(roleId);
            //返回结果
            result.Data = new
            {
                options,
                menuIds
            };
            result.Code = 200;
            result.Msg = "获取成功！";
            result.IsSuccess = true;
            return Json(result);
        }


        #endregion

        #region 菜单列表获取的方法（GetMenuInfos）

        /// <summary>
        /// 用户列表获取的方法
        /// </summary>
        /// <param name="page">第几页</param>
        /// <param name="limit">每页数量</param>
        /// <param name="account">用户账号</param>
        /// <param name="userName">用户姓名</param>
        /// <returns></returns>
        /*[HttpGet]
        public ActionResult GetMenuInfos(int page, int limit, string title)
        {
            //数据总条数
            int count;
            //调用BLL的获取用户方法
            List<GetMenuInfosDTO> list = _menuInfoBLL.GetMenuInfos(page, limit, title, out count);
            //返回结果
            ReturnResult result = new ReturnResult()
            {
                Code = 0,
                Msg = "获取成功！",
                Data = list,
                IsSuccess = true,
                Count = count
            };
            //返回结果
            return Json(result);
        }*/

        #endregion

        #region 绑定用户角色的接口 (BindStaffInfo)


        /// <summary>
        /// 绑定用户角色的方法
        /// </summary>
        /// <param name="userIds"></param>
        /// <param name="roleId"></param>
        /// <returns></returns>
        /// 
        [HttpPost]
        public ActionResult BindStaffInfo(List<string> userIds, string roleId)
        {
            ReturnResult result = new ReturnResult();

            if (string.IsNullOrWhiteSpace(roleId))
            {
                result.Msg = "角色id不能为空！";
                return Json(result);
            }

            //调用绑定的方法
            result.IsSuccess = _roleInfoBLL.BindStaffInfo(userIds, roleId);
            result.Code = 200;
            result.Msg = "成功！";
            return Json(result);

        }
        #endregion

        #region 绑定菜单角色的接口 (BindMenuInfo)


        /// <summary>
        /// 绑定用户角色的方法
        /// </summary>
        /// <param name="userIds"></param>
        /// <param name="roleId"></param>
        /// <returns></returns>
        /// 
        [HttpPost]
        public ActionResult BindMenuInfo(List<string> menuIds, string roleId)
        {
            ReturnResult result = new ReturnResult();

            if (string.IsNullOrWhiteSpace(roleId))
            {
                result.Msg = "角色id不能为空！";
                return Json(result);
            }

            //调用绑定的方法
            result.IsSuccess = _roleInfoBLL.BindMenuInfo(menuIds, roleId);
            result.Code = 200;
            result.Msg = "成功！";
            return Json(result);

        }
        #endregion


    }
}
